package com.itheima.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.attendance.mapper.CompanySettingsMapper;
import com.itheima.attendance.pojo.CompanySettings;
import com.itheima.attendance.service.CompanySettingsService;
import com.itheima.common.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/13 16:47
 */
@Service
@Transactional
public class CompanySettingsServiceImpl implements CompanySettingsService {

    @Autowired
    private CompanySettingsMapper companySettingsMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 制作新的考勤报表
     */
    @Override
    public void newReports(String yearMonth, String companyId) {
        QueryWrapper<CompanySettings> companySettingsQuery =new QueryWrapper<CompanySettings>();
        companySettingsQuery.and(i->i.eq("company_id",companyId).eq("data_month",yearMonth));
        CompanySettings companySettings = companySettingsMapper.selectOne(companySettingsQuery);
        if(companySettings==null){
            //新增
            companySettings=new CompanySettings();
            companySettings.setId(String.valueOf(idWorker.nextId()));
            companySettings.setCompanyId(companyId);
            companySettings.setIsSettings(1);
            companySettings.setDataMonth(yearMonth);
            companySettingsMapper.insert(companySettings);
            return;
        }
        //修改
        companySettingsMapper.update(companySettings,companySettingsQuery);
    }
}
